<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否官方" prop="isTouy">
        <el-select v-model="queryParams.isTouy" placeholder="请选择是否官方" clearable>
          <el-option
              label="是"
              value="Y"
          />
          <el-option
              label="否"
              value="N"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="氧气卡等级" prop="cardLevelId">
        <level-select :key="refreshTime" v-model="queryParams.cardLevelId"></level-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ty:tyUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tyUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="avatar">
        <template #default="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="个人描述" align="center" prop="desc" />
      <el-table-column label="地区" align="center" prop="cityName" />
      <el-table-column label="钱包余额" align="center" prop="balance" />
      <el-table-column label="氧气卡等级" align="center" prop="cardLevelName" />
      <el-table-column label="氧气卡余额" align="center" prop="cardBalance" />
      <!-- <el-table-column label="是否官方" align="center" prop="isTouy">
        <template #default="scope">
          <div class="padRight" >
            <el-switch
                v-model="scope.row.isTouy"
                active-value="Y"
                inactive-value="N"
                :disabled="userStore.userType == 0 ? false: true"
                @change="handleStatusChange(scope.row)"
            ></el-switch>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ty:tyUser:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="tyUserRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TyUser">
import { listTyUser, getTyUser, delTyUser, addTyUser, updateTyUser,changeUserStatus } from "@/api/ty/tyUser";
import useUserStore from '@/store/modules/user'
import {ref} from "vue";
const {proxy} = getCurrentInstance();
const { sys_user_sex } = proxy.useDict('sys_user_sex');
import LevelSelect from "@/components/LevelSelect/index.vue";

const userStore = useUserStore()
const tyUserList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const refreshTime = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nickName: null,
    sex: null,
    mobile: null,
    isTouy:null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户信息列表 */
function getList() {
  loading.value = true;
  listTyUser(queryParams.value).then(response => {
    tyUserList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    nickName: null,
    avatar: null,
    sex: null,
    birthday: null,
    phone: null,
    mobile: null,
    desc: null,
    useMoney: null,
    wxOpenid: null,
    earnMoney: null,
    integral: null,
    account: null,
    level: null,
    vipTime: null,
    openId: null,
    invited: null,
    inviteType: null,
    inviteId: null,
    createTime: null,
    updateTime: null,
    cardLevelId: null,
    deleted: null
  };
  proxy.resetForm("tyUserRef");
  refreshTime.value += 1
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  refreshTime.value += 1
  proxy.resetForm("queryRef");
  queryParams.value.cardLevelId = null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = row.userId || ids.value
  getTyUser(_userId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tyUserRef"].validate(valid => {
    if (valid) {
      if (form.value.userId != null) {
        updateTyUser(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTyUser(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除该数据项？').then(function() {
    return delTyUser(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/tyUser/export', {
    ...queryParams.value
  }, `tyUser_${new Date().getTime()}.xlsx`)
}

/** 用户状态修改  */
function handleStatusChange(row) {
  let text = row.isTouy === "N" ? "取消" : "设置";
  proxy.$modal.confirm('确定要将该用户'+text+ '官方账号吗').then(function () {
    return changeUserStatus(row.userId, row.isTouy);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
    getList();
  }).catch(function () {
    getList();
  });
};

getList();
</script>
